package test {
	import org.aswing.utils.HashMap;

	import flash.display.Bitmap;		

	/**
	 * @author JamesXie
	 * @version 1.0
	 * @date Jan 21, 2011
	 * @name TestLoadList 
	 */
	public class TestLoadList {
		//URL数组 HashMap{[key,url],[key,url]}
		private var _hmURL : HashMap;

		//Image数组 HashMap{[key,image],[key,image]}
		private var _arrImg : HashMap;

		private var _fun : Function;

		/**
		 * 设置下载列表
		 * @param hmURL 
		 * @param fun 当下载全部完成后执行此方法 并返回 fun( HashMap{[key,image],[key,image]} )
		 */
		public function TestLoadList(hmURL : HashMap,fun : Function) : void {
			_hmURL = hmURL;
			
			_fun = fun;
			
			init();
			
			run();
		}

		private function init() : void {
			_arrImg = new HashMap();
		}

		private function run() : void {
			
			if (_hmURL.size() > 0) {
				
				//trace("[DEBUG] [TestLoadList] run");
				
				//trace("hmURL.keys() = " + _hmURL.keys());
				
				var fKey : String = _hmURL.keys().pop();
				
				var fURL : String = _hmURL.remove(fKey);
				
				//trace("fKey00 = " + fKey + " fURL00 " + fURL);

				var fTestLoadImg : TestLoadImg ;
				fTestLoadImg = new TestLoadImg(fKey, fURL, nextJob);
			}else {
				allComplete();
			}
		}

		private function saveImg(key : String = "",bitmap : Bitmap = null) : void {
			_arrImg.put(key, bitmap);
		}

		private function nextJob(key : String = "",bitmap : Bitmap = null) : void {
			//trace("nextJob >>>>>>");
			//trace("key >>> "+key);
			//trace("bitmap >>> "+bitmap);
			
			saveImg(key, bitmap);
			
			if (_hmURL.size() > 0) {
				
				var fKey : String = _hmURL.keys().pop();
				var fURL : String = _hmURL.remove(fKey);
				
				var fTestLoadImg : TestLoadImg;
				fTestLoadImg = new TestLoadImg(fKey, fURL, nextJob);
			}else {
				allComplete();
			}
		}

		private function allComplete() : void {
			_fun(_arrImg);
		}
	}
}